package com.wanxs.mall.member.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.common.utils.HttpUtils;
import com.wanxs.mall.member.dao.MemberDao;
import com.wanxs.mall.member.dto.MemberDTO;
import com.wanxs.mall.member.entity.MemberEntity;
import com.wanxs.mall.member.entity.MemberLevelEntity;
import com.wanxs.mall.member.exception.PhoneExsitExecption;
import com.wanxs.mall.member.exception.UsernameExsitException;
import com.wanxs.mall.member.service.MemberLevelService;
import com.wanxs.mall.member.service.MemberService;
import com.wanxs.mall.member.vo.MemberLoginVO;
import com.wanxs.mall.member.vo.MemberReigerVO;
import com.wanxs.mall.member.vo.SocialUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 会员
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-24
 */
@Service
public class MemberServiceImpl extends CrudServiceImpl<MemberDao, MemberEntity, MemberDTO> implements MemberService {

    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public QueryWrapper<MemberEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void register(MemberReigerVO vo) throws PhoneExsitExecption, UsernameExsitException {
        MemberEntity entity = new MemberEntity();
        // 设置会员等级 默认值
        MemberLevelEntity memberLevelEntity = memberLevelService.queryMemberLevelDefault();
        entity.setLevelId(memberLevelEntity.getId()); // 设置默认的会员等级

        // 添加对应的账号和手机号是不能重复的
        checkUsernameUnique(vo.getUserName());
        checkPhoneUnique(vo.getPhone());

        entity.setUsername(vo.getUserName());
        entity.setNickname(vo.getUserName());
        entity.setMobile(vo.getPhone());

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(vo.getPassword());
        // 需要对密码做加密处理
        entity.setPassword(encode);
        // 设置其他的默认值
        baseDao.insert(entity);
    }


    /**
     * 校验手机号是否存在
     * @param phone
     * @throws PhoneExsitExecption
     */
    private void checkPhoneUnique(String phone) throws PhoneExsitExecption {
        long mobile = baseDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(mobile > 0){
            // 说明手机号是存在的
            throw new PhoneExsitExecption();
        }
    }

    /**
     * 校验账号是否存在
     * @param userName
     * @throws UsernameExsitException
     */
    private void checkUsernameUnique(String userName) throws UsernameExsitException {
        long username = baseDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if(username > 0){
            throw new UsernameExsitException();
        }
    }

    @Override
public MemberEntity login(MemberLoginVO vo) {
    // 1.根据账号或者手机号来查询会员信息
    MemberEntity entity = baseDao.selectOne(new QueryWrapper<MemberEntity>()
            .eq("username", vo.getUserName())
            .or()
            .eq("mobile", vo.getUserName()));
    if(entity != null){
        // 2.如果账号或者手机号存在 然后根据密码加密后的校验来判断是否登录成功
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(vo.getPassword(), entity.getPassword());
        if(matches){
            // 表明登录成功
            return entity;
        }
    }
    return null;
}

    /**
     * 社交登录
     * @param vo
     * @return
     */
    @Override
    public MemberEntity login(SocialUser vo) {
        JSONObject jsonObject = null;
        // 通过token调用微博开发的接口来获取用户的相关信息
        try {
            Map<String,String> query = new HashMap<>();
            query.put("access_token",vo.getAccessToken());
            HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<String, String>(), query);

            if(response.getStatusLine().getStatusCode() == 200){
                String json = EntityUtils.toString(response.getEntity());
                jsonObject = JSON.parseObject(json);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        String uid = jsonObject.getString("login");
        String nickName = jsonObject.getString("name");
        int gender = 1;
//                entity.setNickname(nickName);
//                entity.setGender("m".equals(gender)?1:0);

        // 如果该用户是第一次社交登录，那么需要注册
        // 如果不是第一次社交登录 那么就更新相关信息 登录功能
        MemberEntity memberEntity = baseDao.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", uid));
        if(memberEntity != null){
            // 说明当前用户已经注册过了 更新token和过期时间
            MemberEntity entity = new MemberEntity();
            entity.setId(memberEntity.getId());
            entity.setAccessToken(vo.getAccessToken());
            entity.setExpiresIn(vo.getExpiresIn());
            this.updateById(entity);
            // 在返回的登录用户信息中我们同步的也保存 token和过期时间
            memberEntity.setAccessToken(vo.getAccessToken());
            memberEntity.setExpiresIn(vo.getExpiresIn());
            return memberEntity;
        }
        // 表示用户是第一提交，那么我们就需要对应的来注册
        MemberEntity entity = new MemberEntity();
        entity.setAccessToken(vo.getAccessToken());
        entity.setExpiresIn(vo.getExpiresIn());
        entity.setNickname(nickName);
        entity.setGender(gender);
        entity.setSocialUid(uid);

        // 注册用户信息
        baseDao.insert(entity);
        return entity;
    }
}